/*
	dstack.v
	Data stack module, part of datapath
	
	This project and file(s) are released under GNU GPL v3.
	Please find license file from root directory.
	Meng Sun (c) 2013 <leon.meng.sun@gmail.com>
*/

module dstack (CLK, nRESET, Di, EN, Mode, Do);

input CLK, nRESET, EN, Mode;
input [7:0] Di;
output [7:0] Do;

wire CLK, nRESET, EN, Mode;
wire [7:0] Di;
reg [7:0] Do;

reg [7:0] internalStack [31:0];
reg [4:0] stackPointer;

always @ (posedge CLK or negedge nRESET)
begin
	if (nRESET == 0) /*Async reset*/
	begin
		stackPointer = 5'b00000;
	end
	else if (EN == 1) /*Normal operation*/
	begin
		case (Mode)
		1'b0: begin /*Push*/
			internalStack[stackPointer] = Di;
			stackPointer = stackPointer + 5'b00001;
			Do = 8'bx;
		end
		1'b1: begin /*Pop*/
			stackPointer = stackPointer - 5'b00001;
			Do = internalStack[stackPointer];
		end
		endcase
	end
end

endmodule